package com.xiang.action.impl;

import com.xiang.action.OrderAction;
import com.xiang.doman.Order;
import com.xiang.doman.OrderStatus;
import com.xiang.mapper.OrderMapper;
import io.seata.core.context.RootContext;
import io.seata.rm.tcc.api.BusinessActionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class OrderActionImpl implements OrderAction {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public void tryOrder(BusinessActionContext context, Order order) {
        System.out.println(context);
        System.out.println(RootContext.getXID());
        System.out.println(RootContext.getBranchType());
        System.out.println(RootContext.inGlobalTransaction());
        /**
         * todo  【幂等性、空回滚 、悬挂问题（框架已解决：@TwoPhaseBusinessAction注解配置）
         * 相关: https://seata.apache.org/zh-cn/blog/seata-tcc-fence/#4-tcc-%E5%AD%98%E5%9C%A8%E9%97%AE%E9%A2%98
         */

        orderMapper.insert(order);
    }

    @Override
    public void commit(BusinessActionContext context) {
        log.info("执行了commit");
        Order order=context.getActionContext("order",Order.class);
        order.setStatus(OrderStatus.SUCCESS);
        orderMapper.updateOrderStatus(order);
    }

    @Override
    public void cancle(BusinessActionContext context) {
        log.info("执行了cancle");
        Order order=context.getActionContext("order",Order.class);
        orderMapper.deleteById(order);
    }


}
